﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Xna.Framework.Media;
using ExifLib;
using System.Windows.Media.Imaging;

namespace BigPicWatcher
{
    public partial class AblumList : PhoneApplicationPage
    {
        public AblumList()
        {
            InitializeComponent();

            var library = new MediaLibrary();
            GetAlbums(library.RootPictureAlbum.Albums);
        }

        /// <summary>
        /// Gets Albums from Album collection and adds them to the listing
        /// </summary>
        /// <param name="albumCollection">Collection of Albums</param>
        private void GetAlbums(PictureAlbumCollection albumCollection)
        {
            var albums = new List<AlbumListItem>();

            for (var i = albumCollection.Count() - 1; i >= 0; i--)
            {
                var pictureAlbum = albumCollection[i];

                if (pictureAlbum.Albums.Count > 0)
                    GetAlbums(pictureAlbum.Albums);

                if (pictureAlbum.Pictures.Count == 0)
                    continue;

                var albumListItem = new AlbumListItem { Album = pictureAlbum };
                albumListItem.PictureSelected += albumListItem_PictureSelected;

                albums.Add(albumListItem);
            }

            items.ItemsSource = albums;
        }

        void albumListItem_PictureSelected(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/PictureList.xaml", UriKind.RelativeOrAbsolute));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            MessageBoxResult m = MessageBox.Show("确定要退出吗？", "提醒", MessageBoxButton.OKCancel);

            if (m == MessageBoxResult.Cancel)
            {
                e.Cancel = true;
            }
            else
            {
                while (NavigationService.CanGoBack)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            base.OnBackKeyPress(e);
        }
    }
}